package com.zeta.eduservice.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.poi.hwpf.usermodel.Range;
import org.springframework.stereotype.Component;

import com.zeta.eduservice.entity.question.EduQuestion;
import com.zeta.eduservice.entity.question.EduQuestionChoice;
import com.zeta.eduservice.entity.vo.QuestionUploadVo;
import com.zeta.eduservice.entity.vo.front.front.WordStringInfoVo;

@Component
public class WordAnalysisUtils {
	private int i = 0;
	private int MaxI = 0;
	private int CurrTitleNumber = 0;
	private short TitleNumber1 = 0;
	private QuestionUploadVo chapterId;
	public String  ChoiceNumberTitleRegex=" [A|B|C|D] ";
//public 	String Number1 = "[一二三四五六七八九十]、[\\u4E00-\\u9FA5\\s]{1,}";
	public String Number1Title = "([一|二|三|四|五|六|七|八|九|十]{1,3})([、.]{1})([\\u4E00-\\u9FA5\\s]+题)";
	public String isEmptyRegex="^[\\s&&[^\\n]]\\s*$";
	public String isSimpleNumber2TitleRegex="简要|说明|？|请(讲述|问)|给出|什么|简述";
	public String isSimpleAnswerNumber2TitleRegex="答：";

	public List<EduQuestion> QuestionLists=new ArrayList<EduQuestion>();
	public List<EduQuestionChoice> ChoiceQuestionLists=new ArrayList<EduQuestionChoice>();
	
	
	private Pattern compile;
	private Matcher matcher;
	public void AnalysisWord(Range range, QuestionUploadVo chapterId2) {
		this.chapterId=chapterId2;
		this.MaxI = range.numParagraphs();
		List<WordStringInfoVo> list = new ArrayList<WordStringInfoVo>();
		while (true) {
			boolean titleNumber = getTitleNumber(range, list);
			if (!titleNumber) {
				break;
			}
		}
	}
	private boolean getTitleNumber(Range range, List<WordStringInfoVo> list) {
		String CurrTitle = null;
		CurrTitle = GetNextParagraph(range, CurrTitle);
		String numberTitle = getNumberTitle(CurrTitle);
		if (!numberTitle.equals("")) {
			WordStringInfoVo wordStringInfoVo = new WordStringInfoVo();
			wordStringInfoVo.setNumberTitle(numberTitle);
			list.add(wordStringInfoVo);
			TitleNumber1++;
			CurrTitleNumber = 0;
			CurrTitle = numberTitle;
			if (!CurrTitle.equals(""))
				if (isChoiceTitle(numberTitle)) {
					String ChoiceNumber1 = null;
					while (true) {
						ChoiceNumber1 = GetNextParagraph(range, ChoiceNumber1);
						EduQuestionChoice eduQuestionChoice = new EduQuestionChoice(); 
						boolean titleChoiceNumber = getTitleChoiceNumber(range, ChoiceNumber1,eduQuestionChoice);
						if (!titleChoiceNumber) {
							break;
						}
					}
				} else if (isGapTitle(numberTitle)) {
					String GapNumber1 = null;
					while (true) {
						GapNumber1 = GetNextParagraph(range, GapNumber1);
						EduQuestion eduQuestion = new EduQuestion();
						boolean titleChoiceNumber = getTitleGapNumber(range, GapNumber1,eduQuestion);
						if (!titleChoiceNumber) {
							break;
						}
					}
				} else if (isFalseRightTitle(numberTitle)) {

				} else if (isSimpleTitle(numberTitle)) {
					String SimpleNumber1 = null;
					while (true) {
						SimpleNumber1 = GetNextParagraph(range, SimpleNumber1);
						EduQuestion eduQuestion = new EduQuestion();
						boolean titleNumber = getTitleSimpleNumber(range, SimpleNumber1,eduQuestion);

						if (!titleNumber) {
							break;
						}
					}
				}

		}
		int kk = 0;
		int temp = i;
		while (kk < 6) {
			kk++;
			CurrTitle = GetNextParagraph(range, CurrTitle);
			numberTitle = getNumberTitle(CurrTitle);
			if (numberTitle.equals("")) {
				if (kk == 6) {
					i = temp;
					return false;
				}
			} else {
				i = temp;
				return true;
			}

		}
		return false;

	}

	private boolean isgetSimple(String s) {
		compile = Pattern.compile(isSimpleNumber2TitleRegex);
		matcher = compile.matcher(s);
		if (matcher.find()) {
			
			return true;
		}
		return false;
	}

	private boolean isgetSimpleAnswer(String s) {
		// TODO Auto-generated method stub
		compile = Pattern.compile(isSimpleAnswerNumber2TitleRegex);
		matcher = compile.matcher(s);
		if (matcher.find()) {
			return true;
		}
		return false;
	}

	private boolean getChoice(String s, EduQuestionChoice eduQuestionChoice) {

		compile = Pattern.compile(ChoiceNumberTitleRegex);
		matcher = compile.matcher(s);
		if (matcher.find()) {
			String answer = matcher.group();
			answer = answer.trim();
//			eduQuestionChoice.setContent(s).setAnswer(answer).setChapterId(chapterId);
			return true;
		}
		return false;
	}

	private boolean getTitleGapNumber(Range range, String GapNumber1, EduQuestion eduQuestion) {
		
		String numberTitle = getNumberTitle(GapNumber1);
		if(numberTitle.equals("")) {
			
				eduQuestion.setContent(GapNumber1);
				eduQuestion.setType("填空");
				//eduQuestion.setChapterId(chapterId);
				QuestionLists.add(eduQuestion);
				return true;
			
		}
		
		return false;
	}

	private boolean getTitleSimpleNumber(Range range, String SimpleNumber1, EduQuestion eduQuestion) {
			boolean simple = isgetSimple(SimpleNumber1);
			if (simple) {
			//	eduQuestion.setContent(SimpleNumber1).setChapterId(chapterId);
				
				eduQuestion.setType("简答");
				String answerNumber2 = null;
				answerNumber2 = GetNextParagraph(range, answerNumber2);
				boolean simpleAnswer = isgetSimpleAnswer(answerNumber2);
				if (simpleAnswer) {
					String CurrAnswer = "";
					while (true) {
						CurrAnswer += answerNumber2;
						answerNumber2 = GetNextParagraph(range, answerNumber2);
						String isNumberTitle = getNumberTitle(answerNumber2);
						boolean simple2 = isgetSimple(answerNumber2);
						if (simple2||!isNumberTitle.equals("")) {
							eduQuestion.setAnswer(CurrAnswer);
							
					i--;
							break;
						}
						
					}
					
				}else {
					i--;
				}
				QuestionLists.add(eduQuestion);
				int temp = i;
				int kk = 0;
				while (kk < 6) {
					kk++;
					answerNumber2 = GetNextParagraph(range, answerNumber2);
					boolean is = isgetSimple(answerNumber2);
					if (!is) {
						if (kk == 6) {
							i = temp;
							return false;
						}
					} else {
						i = temp;
						return true;
					}

				}
				
			
			}
			
		
		return false;
	}

	private boolean getTitleFalseRightNumber(Range range, String GapNumber1,EduQuestion eduQuestion) {

		GapNumber1 = getNumberTitle(GapNumber1);
		if (GapNumber1.equals("")) {
		//	eduQuestion.setContent(GapNumber1).setChapterId(chapterId);
			return true;
		}
		return false;
	}

	private boolean getTitleChoiceNumber(Range range, String ChoiceNumber1,EduQuestionChoice eduQuestionChoice) {

		String ChoiceNumber2 = null;
		for (int j = 0; j < 4; j++) {
			ChoiceNumber2 = GetNextParagraph(range, ChoiceNumber2);
			if (j == 0) {
				boolean choice = getChoice(ChoiceNumber1, eduQuestionChoice);

				if (choice) {
					CurrTitleNumber++;
					eduQuestionChoice.setChoiceA(ChoiceNumber2);
				} else {
					return true;
				}
			} else if (j == 1) {
				eduQuestionChoice.setChoiceB(ChoiceNumber2);
			} else if (j == 2) {
				eduQuestionChoice.setChoiceC(ChoiceNumber2);
			} else if (j == 3) {
				eduQuestionChoice.setChoiceD(ChoiceNumber2);
			}

		}
		ChoiceQuestionLists.add(eduQuestionChoice);
		int temp = i;
		int kk = 0;
		while (kk < 6) {
			kk++;
			ChoiceNumber2 = GetNextParagraph(range, ChoiceNumber2);
			boolean choice = isgetChoice(ChoiceNumber2);
			if (!choice) {
				kk++;
				if (kk == 6) {
					i = temp;
					return false;
				}
			} else {
				i = temp;
				return true;
			}

		}
		return false;
	}



	private String GetNextParagraph(Range range, String s) {
		while (true) {
			if (i < MaxI) {
				s = range.getParagraph(i++).text();
				if (isEmpty(s)) {
					continue;
				} else {
					break;
				}
			} else {
				break;
			}

		}
		return s;
	}

	private boolean isGapTitle(String s) {
		String p1 = "填空";
		compile = Pattern.compile(p1);
		matcher = compile.matcher(s);
		if (matcher.find()) {
			return true;
		}
		return false;

	}

	private boolean isFalseRightTitle(String s) {
		String p1 = "判断";
		compile = Pattern.compile(p1);
		matcher = compile.matcher(s);
		if (matcher.find()) {
			return true;
		}
		return false;

	}

	private boolean isSimpleTitle(String s) {
		String p1 = "简答";
		compile = Pattern.compile(p1);
		matcher = compile.matcher(s);
		if (matcher.find()) {
			return true;
		}
		return false;

	}

	private boolean isChoiceTitle(String s) {
		String p1 = "选择";
		compile = Pattern.compile(p1);
		matcher = compile.matcher(s);

		if (matcher.find()) {
			return true;
		}
		return false;

	}

	private boolean isgetChoice(String s) {

		compile = Pattern.compile(ChoiceNumberTitleRegex);
		matcher = compile.matcher(s);
		if (matcher.find()) {
			return true;
		}
		return false;
	}

	private String getNumberTitle(String s) {
		compile = Pattern.compile(Number1Title);
		matcher = compile.matcher(s);
		String s2 = "";
		if (matcher.find()) {
			s2 = s.replaceAll("[一二三四五六七八九十]、", "");
			s2 = s2.replaceAll("（.*）", "");
			return s2;
		}
		return s2;

	}

	private boolean isEmpty(String s) {

		compile = Pattern.compile(isEmptyRegex);
		matcher = compile.matcher(s);

		if (matcher.find()) {
			return true;
		}
		return false;

	}

}
